package com.sxu.fileprocess;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ReadCSV {
	public static String[][] getCsvData(String filePath) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
		String line = "";
		int column = -1;
		ArrayList<String[]> lineList = new ArrayList<String[]>();
		// Read a single line from the file until there are no more lines to
		// read
		while ((line = br.readLine()) != null) {
			String tmpLine = line;
			column = tmpLine.length() - tmpLine.replace(",", "").length() + 1;// csv文件列数
			StringTokenizer st = new StringTokenizer(line, ","); // 以逗号作为分隔符
			String[] currCol = new String[column];
			for (int i = 0; i < column; i++) {
				// 先判断是否还有待读取数据，防止溢出
				if (st.hasMoreTokens()) {
					currCol[i] = st.nextToken();
				}
			}
			lineList.add(currCol);
		}

		String[][] str = new String[lineList.size()][column];
		for (int i = 0; i < lineList.size(); i++) {
			for (int j = 0; j < column; j++) {
				str[i][j] = lineList.get(i)[j];
				//System.out.print(str[i][j] + " ");
			}
			//System.out.println();
		}

		br.close();
		return str;
	}

	public static void main(String[] args) throws IOException {
		new ReadCSV().getCsvData(
				"C:\\Users\\FELIX\\Desktop\\file20181217\\camx_溯源数据_2018120908_2018121908\\CZBZ\\CZBZ_NO2_boiler_2018120908_2018121908.csv");
	}
}
